package org.hyx.topic2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyInterceptor {

    //将公共切点部分抽取出来
    @Pointcut("execution(public * org.hyx.topic2.service.impl.StudentServiceImpl.*(..))")
    public void StudentServiceImpl() {}

    @Before("execution(public * org.hyx.topic2.service.impl.StudentServiceImpl.*(..))")
    public void beforeDo() {
        System.out.println("[前置通知]");
    }

    @After("execution(public * org.hyx.topic2.MyInterceptor.*(..))")
    public void afterDo() {
        System.out.println("[最终通知]");
    }

    @AfterReturning("StudentServiceImpl()")
    public void returnDo() {

        System.out.println("[后置通知]");
    }

    @AfterThrowing("StudentServiceImpl()")
    public void exceptionDo() {
        System.out.println("[异常通知]方法抛出异常");
    }

    @Around("StudentServiceImpl()")
    public void aroundDo(ProceedingJoinPoint pjp) throws Throwable {
        //ProceedingJoinPoint的getSignature()方法用于获取切入点
        System.out.println("环绕通知:开始执行[" + pjp.getSignature().getName() + "]方法！");
        Object res = null;
        res = pjp.proceed();
        System.out.println("环绕通知:结束执行[" + pjp.getSignature().getName() + "]方法！");
    }

}
